package io.vertx.examples.spring.common;


import com.xiaoleilu.hutool.date.DateUnit;
import com.xiaoleilu.hutool.date.DateUtil;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateTimeUtils {

    static Logger logger = LoggerFactory.getLogger(DateTimeUtils.class);

    public static final String YYYYMMDD = "yyyy-MM-dd";

    /**
     * 没有分隔符的年月日
     */
    public static final String YYYYMMDD_NO_SPLIT = "yyyyMMdd";

    public static final String YYYYMMDDHHMMSS_SPLIT = "yyyy-MM-dd HH:mm:ss";

    public static final String HHMMSS_SPLIT = "HH:mm:ss";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";


    /**
     * 根据格式 获取当前 字符串日期
     *
     * @param pattern
     * @return
     */
    public static String now(String pattern){
        return DateTime.now().toString(pattern);
    }

    /**
     * 根据格式 获取当前 字符串日期
     *
     * @param pattern
     * @return
     */
    public static String parseStr(String pattern, Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 根据格式 获取当前 字符串日期
     *
     * @param pattern
     * @return
     */
    public static Date parseDate(String pattern, String date){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(date);
        } catch (ParseException e) {
            logger.error("日期解析错误",e);
        }
        return null;
    }

    /**
     * 获取时间的 时分秒的毫秒数
     *
     gethour()*3600+getMinute()*60 + getSecond()
     set as long seconds
     compare them
     * @param date
     * @return
     */
    public static long getHMSMill(Date date){
        logger.info("小时 {} 分钟 {} 秒数 {}",date.getHours(),date.getMinutes(),date.getSeconds());
        long second = date.getHours()*3600+date.getMinutes()*60 + date.getSeconds();
        return second;
    }

    /**
     * 获取时间的 时分秒的毫秒数
     *
     gethour()*3600+getMinute()*60 + getSecond()
     set as long seconds
     compare them
     * @param dateStr
     * @param pattern
     * @return
     */
    public static long getHMSMill(String dateStr,String pattern){
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            date = sdf.parse(dateStr);

            logger.info("小时 {} 分钟 {} 秒数 {}",date.getHours(),date.getMinutes(),date.getSeconds());
            long second = date.getHours()*3600+date.getMinutes()*60 + date.getSeconds();
            return second;
        } catch (ParseException e) {
            logger.error("日期解析错误",e);
        }
        return 0;
    }
    /**
     * 根据格式 获取当前 字符串日期
     *
     * @param date          时间
     * @param sourcePattern 源格式
     * @param targetPattern 目标格式
     * @return
     */
    public static String parse(String date,String sourcePattern,String targetPattern){
        SimpleDateFormat sdf = new SimpleDateFormat(sourcePattern);
        String resultDate = "";
        try {
            Date sourceDate = sdf.parse(date);

            sdf.applyPattern(targetPattern);
            resultDate = sdf.format(sourceDate);
        } catch (ParseException e) {
            logger.error("日期解析错误",e);
            e.printStackTrace();
        }
        logger.info("从 {} 转换到 {}",date,resultDate);
        return resultDate;
    }

    /**
     * 判断是否 时分秒 sourceTime > targetTime
     * @param sourceTime
     * @param targetTime
     * @return
     */
    public static boolean largeThan(Date sourceTime, Date targetTime) {
        return  sourceTime.getTime() > targetTime.getTime();
    }

    /**
     * 时间纠错
     *
     * 三天之内都用create_time,超过则用 现在的时间
     *
     * @param create_time
     * @return
     */
    public static String timeCalibrate(String create_time,int times) {
        Date now = new Date();
        Date createTime = DateTimeUtils.parseDate(DateTimeUtils.YYYYMMDDHHMMSS,create_time);
        long days = DateUtil.between(now,createTime, DateUnit.DAY);
        if (days > times){
            logger.info("create_time {} 超过{}天",create_time,times);
            return DateTimeUtils.parseStr(DateTimeUtils.YYYYMMDDHHMMSS_SPLIT,now);
        }else{
            return DateTimeUtils.parseStr(DateTimeUtils.YYYYMMDDHHMMSS_SPLIT,createTime);
        }

    }

    /**
     * 时间纠错
     *
     * 三天之内都用create_time,超过则用 现在的时间
     *
     * @param create_time
     * @return
     */
    public static String timeCalibrate(String create_time,int times,String pattern) {
        Date now = new Date();
        Date createTime = DateTimeUtils.parseDate(DateTimeUtils.YYYYMMDDHHMMSS,create_time);
        long days = DateUtil.between(now,createTime, DateUnit.DAY);
        if (days > times){
            logger.info("create_time {} 超过{}天",create_time,times);
            return DateTimeUtils.parseStr(pattern,now);
        }else{
            return DateTimeUtils.parseStr(pattern,createTime);
        }

    }

}
